package com.glodon.paas.search.parser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.glodon.paas.exception.PaasServerExceptionFactory;

/**
 * 文本内容解析器.
 * 
 * @author botao.liao
 * 
 */
@Component("officeTextParser")
public class OfficeTextParser implements TextParser {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public List<Map> parse(InputStream iStream) {
        List<Map> result = new ArrayList<Map>();
        try {
            byte[] bytes = IOUtils.toByteArray(iStream);
            logger.debug("read {} bytes from stream", bytes.length);
            String content = Base64.encodeBase64String(bytes);
            Map mContent = new HashMap();
            mContent.put("content", content);
            result.add(mContent);
        } catch (IOException e) {
            logger.error("failed to read context.txt.", e);
            throw PaasServerExceptionFactory.systemException("failed to read context.txt.", e);
        } finally {
            IOUtils.closeQuietly(iStream);
        }
        return result;
    }

}
